package src.mian.java;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class HDFSDirectoryCreateDelete {
    public static void main(String[] args) {
        String hdfsUri = "hdfs://192.168.254.100:8020"; // 替换成您的 HDFS URI
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", hdfsUri);
        try {
            FileSystem fs = FileSystem.get(conf);
            // 创建目录
            Path createDirectoryPath = new Path("/car/price"); // 替换成您要创建的目录路径
            boolean created = fs.mkdirs(createDirectoryPath);
            if (created) {
                System.out.println("Directory created: " + createDirectoryPath);
            } else {
                System.out.println("Failed to create directory: " + createDirectoryPath);
            }
            // 删除目录
            Path deleteDirectoryPath = new Path("/car"); // 替换成您要删除的目录路径
            boolean deleted = fs.delete(deleteDirectoryPath, true); // 第二个参数表示是否递归删除目录
            if (deleted) {
                System.out.println("Directory deleted: " + deleteDirectoryPath);
            } else {
                System.out.println("Failed to delete directory: " + deleteDirectoryPath);
            }

            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
